package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.POST;
import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {

    @Autowired
    private VideoManager videoManager;

    //视频列表
    @GetMapping("/smallVideos")
    public ResponseEntity findRecommendVideoVoByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        Long userId = UserHolder.get().getId();

        return videoManager.findRecommendVideoVoByPage(userId, pageNum, pageSize);
    }
    //发布视频
    @PostMapping("/smallVideos")
    public ResponseEntity publishVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        Long userId = UserHolder.get().getId();

      return  videoManager.publishVideo(userId, videoThumbnail, videoFile);
    }

    //关注用户
    @PostMapping("/smallVideos/{focusUserId}/userFocus")
    public void saveFocusUser(@PathVariable Long focusUserId) {
        Long userId = UserHolder.get().getId();
        videoManager.saveFocusUser(userId, focusUserId);
    }

    //取消关注
    @PostMapping("/smallVideos/{focusUserId}/userUnFocus")
    public void removeFocusUser(@PathVariable Long focusUserId) {
        Long userId = UserHolder.get().getId();

        videoManager.removeFocusUser(userId, focusUserId);
    }


    //点赞
    @PostMapping("/smallVideos/{publishId}/like")
    public ResponseEntity videoLike(@PathVariable String publishId) {
        System.out.println(publishId);

        return videoManager.videoLike(publishId);
    }

    //取消点赞
    @PostMapping("/smallVideos/{publishId}/dislike")
    public ResponseEntity videoDislike(@PathVariable String publishId) {

        return videoManager.videoDislike(publishId);
    }

    //获取评论列表
    @GetMapping("/smallVideos/{publishId}/comments")
    public ResponseEntity findCommentByPage(@PathVariable String publishId,
                                            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return videoManager.findCommentByPage(publishId, pageNum, pageSize);
    }

    //发布评论
    @PostMapping("/smallVideos/{publishId}/comments")
    public ResponseEntity videoComment(@PathVariable String publishId, @RequestBody Map comment) {
        System.out.println(comment);
        String comment1 = (String) comment.get("comment");
        return videoManager.videoComment(publishId, comment1);
    }

    //评论点赞
    @PostMapping("/smallVideos/comments/{publishId}/like")
    public ResponseEntity commentLike(@PathVariable String publishId) {
        return videoManager.commentLike(publishId);
    }

    //评论取消点赞
    @PostMapping("/smallVideos/comments/{publishId}/dislike")
    public ResponseEntity commentDislike(@PathVariable String publishId) {
        return videoManager.commentDislike(publishId);
    }



}
